package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.cultivate.entity.response.CourseConsumptionRate;
import com.xmy.cultivate.mapper.CourseConsumptionRateMapper;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/courseConsumptionRate")
public class CourseConsumptionRateController extends BaseController {
    @Autowired
    CourseConsumptionRateMapper courseConsumptionRateMapper;

    /**
     * 消课率列表
     */
    @GetMapping("/list")
    public Result list(@RequestParam Map<String, String> reMap) {
        String startDate = reMap.get("startDate");
        String endDate = reMap.get("endDate");
        String yearPart = reMap.get("yearPart");
        String quarterNum = reMap.get("quarterNum");
        String schoolId = reMap.get("schoolId");
        String yearClassId = reMap.get("yearClassId");
        String classTypeId = reMap.get("classTypeId");
        String subjectsId = reMap.get("subjectsId");
        String type = reMap.get("type") != null && !reMap.get("type").isEmpty() ? reMap.get("type") : "1";

        if (StringUtils.isBlank(schoolId)) {
            if (type.equals("2")) {
                //校区老师状态，只能看自己所在校区的数据
                schoolId = this.baseSchoolIdList.stream().map(Object::toString).collect(Collectors.joining(","));
            }
        }

        List<CourseConsumptionRate> courseConsumptionRateList = courseConsumptionRateMapper.getCourseConsumptionRateList(type, startDate, endDate, yearPart, quarterNum, schoolId,yearClassId,classTypeId,subjectsId);
        return new Result(Code.OK, new PageResult<>(courseConsumptionRateList, (long) courseConsumptionRateList.size(), 1L));
    }

    /**
     * 消课率详情
     */
    @GetMapping("/details")
    public Result details(@RequestParam Map<String, String> reMap) {
        String startDate = reMap.get("startDate");
        String endDate = reMap.get("endDate");
        String yearPart = reMap.get("yearPart");
        String quarterNum = reMap.get("quarterNum");
        String schoolId = reMap.get("schoolId");
        String teacherId = reMap.get("teacherId");
        String yearClassId = reMap.get("yearClassId");
        String classTypeId = reMap.get("classTypeId");
        String subjectsId = reMap.get("subjectsId");
        List<CourseConsumptionRate> courseConsumptionRateList = courseConsumptionRateMapper.getCourseConsumptionRateDetails(startDate, endDate, yearPart, quarterNum, schoolId, teacherId,yearClassId,classTypeId,subjectsId);
        return new Result(Code.OK, new PageResult<>(courseConsumptionRateList, (long) courseConsumptionRateList.size(), 1L));
    }

}
